//trapbox.txt - A trapped box. You can place this trap on any container which is
// opened/closed (boxes, lockers, etc).
//Cell 0 - Difficulty of trap.
//Cell 1 - The effect created. 
//Cell 2,3 - The sdf which is set to 1 when box is unlocked.

// Cell 5 - If nonzero, this is a boobytrap. Goes off instantly

// effects
//50 - big fire boom, no damage
//51 - 20 damage
//52 - 30 damage
//53 - 80 damage
//54 - 200 damage
//55 - 1000 damage

//60 - big electric boom, no damage
//61 - 20 damage
//62 - 50 damage
//63 - 100 damage
//64 - 250 damage
//65 - 1000 damage

//70 - big cold boom, no damage
//71 - 20 damage
//72 - 40 damage
//73 - 120 damage
//74 - 300 damage
//75 - 1000 damage

beginobjectscript;

variables;
	short i_am_open = 0;
	short trap_is_gone = 0;
	short got_warning = 0;
	short r1;
	short trap_level = 6;

body;

beginstate INIT_STATE;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
			set_obj_tool_difficulty(ME,0);
		}
	if (get_memory_cell(4) > 0)	
		trap_level = get_memory_cell(4);
	if (get_memory_cell(5) > 0)	
		got_warning = 1;
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE;

break;

beginstate USE_STATE;
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
		if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
			trap_is_gone = 1;
		}
	
	if (is_combat()) {
		print_str_color("Open Box: You can't disarm traps during combat.",1);
		end();
		}
		
	if (trap_is_gone == 0) {
		if (get_stat_total(24) >= get_memory_cell(0)) {

			print_str("Open Box: It has a trap, but you are able to disarm it.");
			trap_is_gone = 1;

			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
				if (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)
					award_party_xp(BASE_TRAP_XP,get_memory_cell(0));
				set_flag(get_memory_cell(2),get_memory_cell(3),1);
				}
			}
			else if (got_warning == 0) {
				print_str("Open Box: You are able to tell that it has a trap, but your");
				print_str("  Tool Use skill isn't high enough to disarm it. Try again to");
				print_str("  open it anyway.");
				got_warning = 1;
				end();
				}
				else { // trigger trap
					if (get_memory_cell(5) > 0)	
						print_str("Open Box: The box is boobytrapped!");
						else print_str("Open Box: You set off a trap!");
					trap_is_gone = 1;
					if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
						set_flag(get_memory_cell(2),get_memory_cell(3),1);
					run_sparkles_on_object(ME,get_memory_cell(1),1,0);
					end();
					}
		}		
		
	if (i_am_open == 0) {
		i_am_open = 1;
		open_container();
		}
		else {
			close_container();
			i_am_open = 0;
			}
break;


beginstate UNLOCK_STATE;
	if (trap_is_gone == 0) {
		print_str("Unlock Spell: A container glows red. It must have a trap.");
		}
break;

